package com.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srb.core.enums.BorrowerStatusEnum;
import com.srb.core.mapper.BorrowerAttachMapper;
import com.srb.core.mapper.BorrowerMapper;
import com.srb.core.mapper.UserInfoMapper;
import com.srb.core.pojo.entity.Borrower;
import com.srb.core.pojo.entity.BorrowerAttach;
import com.srb.core.pojo.entity.UserInfo;
import com.srb.core.pojo.vo.BorrowerAttachVo;
import com.srb.core.pojo.vo.BorrowerDetailVo;
import com.srb.core.pojo.vo.BorrowerVo;
import com.srb.core.service.BorrowerAttachService;
import com.srb.core.service.BorrowerService;
import com.srb.core.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-08-08
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BorrowerAttachMapper borrowerAttachMapper;

    @Autowired
    private DictService dictService;

    @Autowired
    private BorrowerAttachService borrowerAttachService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBorrowerVoByUserId(BorrowerVo borrowerVo, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        //1、保存借款人信息
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVo,borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        baseMapper.insert(borrower);
        //2、保存附件信息
        List<BorrowerAttach> borrowerAttachList = borrowerVo.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            borrowerAttach.setBorrowerId(borrower.getId());
            borrowerAttachMapper.insert(borrowerAttach);
        }
        //3、更新userInfo借款人认证状态
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public Integer getStatusByUserId(Long userId) {
        QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("status").eq("user_id",userId);
        List<Object> objects = baseMapper.selectObjs(queryWrapper);
        if (objects.size() == 0)
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        return (Integer) objects.get(0);
    }

    @Override
    public IPage<Borrower> listPage(Page<Borrower> pageParam, String keyword) {
        if (StringUtils.isBlank(keyword)){
            return baseMapper.selectPage(pageParam,null);
        }
        keyword = keyword.trim();
        QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword)
                .or().like("id_card",keyword)
                .or().like("mobile",keyword)
                .orderByDesc("id");
        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public BorrowerDetailVo getBorrowerDetailById(Long id) {
        //获取借款人信息
        Borrower borrower = baseMapper.selectById(id);
        //填充基本借款人信息
        BorrowerDetailVo detailVo = new BorrowerDetailVo();
        BeanUtils.copyProperties(borrower,detailVo);
        detailVo.setMarry(borrower.getMarry()? "是":"否");
        detailVo.setSex(borrower.getSex() == 1? "男":"女");
        detailVo.setAge(borrower.getAge().toString());
        //下拉列表
        detailVo.setEducation(dictService.getNameByParentDicCodeAndValue("education",borrower.getEducation()));
        detailVo.setIndustry(dictService.getNameByParentDicCodeAndValue("industry",borrower.getIndustry()));
        detailVo.setIncome(dictService.getNameByParentDicCodeAndValue("income",borrower.getIncome()));
        detailVo.setReturnSource(dictService.getNameByParentDicCodeAndValue("returnSource",borrower.getReturnSource()));
        detailVo.setContactsRelation(dictService.getNameByParentDicCodeAndValue("relation",borrower.getContactsRelation()));
        //审批状态
        Integer status = borrower.getStatus();
        detailVo.setStatus(BorrowerStatusEnum.getMsgByStatus(status));
        //附件列表
        List<BorrowerAttachVo> voList = borrowerAttachService.selectBorrowerAttachVoList(id);
        detailVo.setBorrowerAttachVoList(voList);
        return detailVo;
    }
}
